<?php
/*
	@author   ducminh_ajax <ducminh_ajax@yahoo.com.vn>
	@date 05/11/2011
	@update:
*/
 
class CrmCustomerController extends Zone_Action{
	public function field(){
		$data = array(
			date_active =>array(
				type			=> 'NONE',
				default_value   => new Model_Expr('NOW()')
			),
			date_updated =>array(
				type			=> 'NONE',
				default_value   => new Model_Expr('NOW()')
			),
			updated_by_id =>array(
				type			=> 'NONE',
				default_value 	=> get_user_id()
			),
						
			code 	=> array(
				type 		=> 'CHAR',
				no_empty 	=>	true,
				form_label	=> 'Mã công ty',			
			),
			name 	=> array(
				type 			=> 'CHAR',
				no_empty 		=> true,
				form_label		=> 'Tên KH'
			),
			address 	=> array(
				type 			=> 'CHAR',
				form_label		=> 'Địa chỉ'
			),
			email 	=> array(
				type 			=> 'EMAIL',
				form_label		=> 'Email'
			),
			birthday 	=> array(
				type 			=> 'DATE',
				form_label		=> 'Sinh nhật công ty',
				fix_value		=> change_date_format
			),
			phone 	=> array(
				type 			=> 'CHAR',
				form_label		=> 'Phone'
			),
			fax 	=> array(
				type 			=> 'CHAR',
				form_label		=> 'Fax'
			),
			website 	=> array(
				type 			=> 'CHAR',
				form_label		=> 'Website'
			),
			desc 	=> array(
				type 			=> 'TEXT',
				form_label		=> 'Mô tả'
			),
			trade_id 	=> array(
				type 			=> 'INT',
				form_label		=> 'Ngành nghề'
			),
			parent_id 	=> array(
				type 			=> 'INT',
				form_label		=> 'Nhóm KH'
			),
			country_id 	=> array(
				type 			=> 'INT',
				form_label		=> 'Quốc gia'
			),
			state_id 	=> array(
				type 			=> 'INT',
				form_label		=> 'Tỉnh thành'
			),
			district_id 	=> array(
				type 			=> 'INT',
				form_label		=> 'Quận huyện'
			),
			care_by_id 	=> array(
				type 			=> 'INT',
				form_label		=> 'Chăm sóc'
			)		
		);
		return $data;	
	}
	
	public function indexAction(){
		$user_id = get_user_id();
		
		$vars  = array();		
		$order_by = " ORDER BY `ID` DESC";
		
		$current_page = max(getInt('p',1),1);		
		$vars['p'] = $current_page;		
		$range_page = max(10,(int)$this->getConfig('user.page',20));				
		$limit = "LIMIT ".($current_page-1)*$range_page.",$range_page";
				
		$field_orders = 
			array(
				'`a`.`ID`'					=>	'ID',
				'`a`.`code`' 				=>	'code',
				'`a`.`name`'				=>	'name',
				'`a`.`phone`'				=>	'phone',
				'`a`.`email`'				=>	'email',
				'`a`.`desc`'				=>	'desc',
				'`a`.`fax`'					=>	'fax',				
				'`a`.`care_by_id`'			=>	'care_by_id',
				'`a`.`website`'				=>	'website',
				'`a`.`address`'				=>	'address',
				'`a`.`care_by_id`'			=>	'care_by_id',
				'`a`.`parent_id`' 			=>	'parent_id',
				'`a`.`trade_id`'			=>	'trade_id'
			);
			
		$field_groups = 
			array(
				'`a`.`care_by_id`'			=>	'care_by_id',
				'`a`.`parent_id`'			=> 	'parent_id',
				'`a`.`trade_id`'			=>	'trade_id',  
				'`a`.`country_id`'			=>	'country_id',
				'`a`.`state_id`'			=>	'state_id',
				'`a`.`district_id`'			=>	'district_id'
			);
			
		$field_texts = 
			array(
				'`a`.`code`'				=> 'code',
				'`a`.`name`'				=> 'name',
				'`a`.`phone`'				=> 'phone',
				'`a`.`email`'				=> 'email',
				'`a`.`desc`'				=> 'desc',
				'`a`.`fax`'					=> 'fax',
				'`a`.`website`'				=> 'website',
				'`a`.`address`'				=> 'address'
			);
		
		foreach( $field_orders as $f => $r ){
			if( get('order_by') == $r ){
				$order_type = ( get('order_type') == 'desc' ? 'desc' : 'asc');
				$order_by = "ORDER BY $f $order_type";
				$vars['order_by'] = $r;
				$vars['order_type'] = $order_type;
				break;	
			}	
		}
		
		$where = array("`a`.`is_draft`='no'");
		
		$word = get('s','');
		if( $word != '' ){
			$a = array();
			foreach( $field_texts as $k => $f ){
				$a[] = app_search_sql_text($k, $word);
			}			
			$vars['s'] = $_REQUEST['s'];
			$where[]   = '('.implode(' OR ',$a).')';			
		}

		
		foreach( $field_texts as $k => $f ){
			$word = get($f,'');
			if( $word == '' ) continue;
			$where[] = app_search_sql_text($k, $word);
			$vars[$f] = $_REQUEST[$f];
		}			

		foreach( $field_groups as $k => $f ){
			if( is_array($_REQUEST[$f]) ){
				$s = implode(',',get($f,array(),true));
				$vr = implode(',', $_REQUEST[$f]);
			}else{
				$s = get($f,'');
				$vr = $_REQUEST[$f];
			}	
			if( $s != '' ){
				$where[]= app_search_sql_group( $k, $s);
				$vars[$f] = $vr;
			}	
		}		
		
		$where = implode(' AND ',$where);

		$a = $vars;
		unset($a['p']);
		if( $this->getAction() == 'Search' ){
			return $a;
		}
		
		$total = $this->Model->fetchOne("SELECT COUNT(*) 
			FROM `crm_customers` as `a` WHERE $where");
		
		$posts = $this->Model->fetchAll("SELECT 
			`a`.*,
			`b`.`username` 	 as `care_by_name`,
			`b`.`is_deleted` as `care_is_deleted`
			FROM `crm_customers` as `a` 
			LEFT JOIN `users` as `b`
				ON `a`.`care_by_id` = `b`.`ID`
			WHERE $where $order_by $limit");
		
		
		$page = page_ajax('#Crm/Customer?'.to_query_configs($a,false).'&p=', $total , $range_page, $current_page);	
		
		$cats = $this->Model->fetchAll('crm_customer_types',"SELECT * FROM `crm_opportunity_types` ORDER BY `title`");			
		
		$this->set(array(
			user_id =>get_user_id(),
			page	=> $page,
			total	=> $total,
			vars	=> $vars,
			posts	=> $posts,
			cats	=> $cats	
		));
	}

	public function uploadAction(){
		$this->removeLayout();
		$post_id = getInt('post_id',0);
		$user_id = get_user_id();
		
		$post = $this
			->Model
			->fetchRow("SELECT * FROM `crm_customers` WHERE `ID`='$post_id'");
		
		if(!$post){
			return $this->setContent(error('Bản ghi đã bị xóa hoặc không tồn tại'));
		}	

		if( $post['created_by_id'] != $user_id && !$this->hasRole('crm_admin') 
				&& !( $post['care_by_id'] == $user_id && $post['is_draft']=='no' ) ){
			return $this->setContent(error('Bạn không có quyền sửa bản ghi này'));			
		}						
		
		if( isPost() ){
			loadClass('ZData');
			$f = new ZData();
			$f->addField(array(
				file => array(
					type 		=> 'FILE',
					path		=> 'files/crm/',
					no_empty	=> true
				)
			));
			
			$data = $f->getData();
			if( !is_array( $data ) ){
				return $this->setJSON(array(
					alert => error($data)
				));				
			}
			
			$file = $data['file'];
			$this->Model->insert('crm_files',array(
				post_id				=> $post_id,
				name				=> $file[0],
				type				=> $file[1],
				size				=> $file[2],
				filename			=> $file[3],
				mode				=> 'customer'		
			));
			
			$file_id = $this->Model->lastId();				
			return $this->setJSON(array(
				content	=> "<div class='x-file-info'>
					<input checked type='checkbox' name='files[]' value='$file_id'/>
					<a href='".ctrUrl($this)."/File?ID=$file_id' target='_blank'>{$file[3]} {$file[2]} KB	</a>
				</div>"
			));
		}
	}

	private function getOptions(){
		$trades = $this->Model->fetchAll('crm_trade_types',"SELECT * FROM `crm_trade_types` ORDER BY `title`");
		$countries = $this->Model->fetchAll('list_countries',"SELECT * FROM `locations` WHERE `type`='1' ORDER BY `title`");
		$cats = $this->Model->fetchAll('crm_customer_types',"SELECT * FROM `crm_customer_types` ORDER BY `title`");
		$this->set(array(
			trades		=> $trades,
			countries	=> $countries,
			cats		=> $cats
		));
	}	
	
	public function validData( $data ){
		if( !is_array( $data ) )
			return $data;
		return $data;
	}
	
	public function validcodeAction(){
		$code = get('code');
		$check = $this->Model->fetchRow("SELECT * FROM `crm_customers` WHERE `code`='$code'");
		die('0');//$check ? 1 : 0 );
	}
	
	public function addAction(){
		$user_id = get_user_id();	
	
		if( !isPost() ){
			$post = $this->Model->fetchRow("SELECT * 
				FROM `crm_customers` 
				WHERE 
					`created_by_id`='$user_id' 
					AND `is_draft`='yes'");
			
			$post_id = $post['ID'];			
			
			if( !$post ){
				$this->Model->insert('crm_customers',array(
					created_by_id	=>	get_user_id(),
					date_created 	=> 	new Model_Expr('NOW()')
				));
				
				$post_id = $this->Model->lastId();				
			}
			
			$this->getOptions();			
			
			$this->set(array(
				ID	=> $post_id
			));		
		
		}else{		
		
			$post_id = getInt('ID',0);
			if( !$this->Model->fetchRow("SELECT * FROM `crm_customers` WHERE `created_by_id`='$user_id' AND `ID`='$post_id'") ){
				$this->setJSON(array(
					message	=> error('Bản ghi đã bị xóa hoặc không tồn tại')
				));
			}
		
			loadClass('ZData');
			$f = new ZData();
			$f->addFields( $this->field());
			
			$data = $f->getData();
			$data = $this->validData( $data );
			
			if( !is_array($data)){
				return $this->setJSON(array(
					message => error($data)
				));
			}		

			$data['date_created']  = new Model_Expr('NOW()');
			$data['is_draft']='no';
			
			$this->Model->update('crm_customers',$data,"`ID`='$post_id'");
			
			//file hadler
			$files_selected = get('files',array(),true);
			$files = $this->Model->fetchAll("SELECT * FROM `crm_files` 
				WHERE `post_id`='$post_id' AND `mode`='customer'");
			foreach( $files as $f ){
				if( !in_array( $f['ID'], $files_selected ) ){
					$this->Model->delete('crm_files',"`ID`='{$f['ID']}'");
					@unlink("files/crm/{$f['name']}");
				}
			}			
				
			return $this->setJSON(array(
				redirect	=> "#Crm/Customer/View?ID=$post_id"
			));
		}
	}

	public function editAction(){
		$post_id = getInt('ID',0);
		$user_id = get_user_id();
				
		$post = $this
			->Model
			->fetchRow("SELECT * FROM `crm_customers` WHERE `ID`='$post_id' AND `is_draft`='no'");
		
		if(!$post){
			return $this->setContent(error('Bản ghi đã bị xóa hoặc không tồn tại'));
		}	

		if( $post['created_by_id'] != $user_id AND !$this->hasRole('crm_admin') AND $post['care_by_id'] != $user_id){
			return $this->setContent(error('Bạn không có quyền sửa bản ghi này'));			
		}				
		
		if( !isPost() ){	
			
			$this->getOptions();			
			$this->set(array(
				ID	 => $post_id,
				post => $post
			));
			
			if( $post['country_id'] ){
				$states = $this->Model->fetchAll("SELECT * FROM `locations` 
					WHERE `type`='2' AND `parent_id`='{$post['country_id']}' ORDER BY `title`");
				$this->set('states', $states);					
			}

			if( $post['state_id'] ){
				$state = $this->Model->fetchAll("SELECT * FROM `locations` 
					WHERE `type`='2' AND `ID`='{$post['state_id']}'");
				if( $state ){
					$districts = $this->Model->fetchAll("SELECT * FROM `locations` 
						WHERE `type`='3' AND `parent_id`='{$post['state_id']}' ORDER BY `title`");
					$this->set('districts', $districts);					
				}	
			}
			
			$files = $this->Model->fetchAll("SELECT * FROM `crm_files` WHERE `mode`='customer' AND `post_id`='$post_id'");
			$this->set(array(
				files => $files
			));
			
			$care = $this->Model->fetchRow("SELECT `username`,`ID`,`is_deleted` 
				FROM `users` WHERE `ID`='{$post['care_by_id']}'");
			
			if( $care ){
				$this->set(array(
					care => $care
				));
			}
			
		}else{
			loadClass('ZData');
			
			$f = new ZData();
			$f->addFields( $this->field());

			$data = $f->getData();
			$data = $this->validData( $data );
			
			if( !is_array($data)){
				return $this->setJSON(array(
					message => error($data)
				));
			}		

			$data['date_created']  = new Model_Expr('NOW()');
			$data['is_draft']='no';
			
			$this->Model->update('crm_customers',$data,"`ID`='$post_id'");
			
			//file hadler
			$files_selected = get('files',array(),true);
			
			$files = $this->Model->fetchAll("SELECT * FROM `crm_files` 
				WHERE `post_id`='$post_id' AND `mode`='customer'");
				
			foreach( $files as $f ){
				if( !in_array( $f['ID'], $files_selected ) ){
					$this->Model->delete('crm_files',"`ID`='{$f['ID']}'");
					@unlink("files/crm/{$f['name']}");
				}
			}			
				
			return $this->setJSON(array(
				redirect => "#Crm/Customer/View?ID=$post_id"
			));
		}
	}

	public function viewAction(){
		$post_id = getInt('ID',0);
		
		$post  = $this->Model->fetchRow("
			SELECT `a`.*,
					`b`.`username` as `created_by_name`,
					`b`.`is_deleted` as `created_is_deleted`,
					`c`.`username` as `updated_by_name`,
					`c`.`is_deleted` as `updated_is_deleted`,
					`d`.`title` as `trade_title`, 
					`e`.`title` as `parent_title`, 
					`f`.`title` as `country_title`, 
					`g`.`title` as `state_title`, 
					`h`.`title` as `district_title` 
			FROM `crm_customers` as `a`
				LEFT JOIN `users` as `b` 
					ON `b`.`ID`=`a`.`created_by_id`
				LEFT JOIN `users` as `c` 
					ON `c`.`ID`=`a`.`updated_by_id`
				LEFT JOIN `crm_trade_types` as `d` 
					ON `d`.`ID`=`a`.`trade_id`
				LEFT JOIN `crm_customer_types` as `e` 
					ON `e`.`ID`=`a`.`parent_id`
				LEFT JOIN `locations` as `f` 
					ON `f`.`ID`=`a`.`country_id` AND `f`.`type`='1'
				LEFT JOIN `locations` as `g` 
					ON `g`.`ID`=`a`.`state_id` AND `g`.`type`='2'
				LEFT JOIN `locations` as `h` 
					ON `h`.`ID`=`a`.`district_id` AND `h`.`type`='3'
			WHERE `a`.`ID`='$post_id' AND `a`.`is_draft`='no'");
			
		if(!$post){
			return $this->setContent(error('Bản ghi đã bị xóa hoặc không tồn tại'));
		}
		
		$files = $this->Model->fetchAll("SELECT * FROM `crm_files` WHERE `mode`='customer' AND `post_id`='$post_id'");
		
		$contacts = $this->Model->fetchAll("SELECT 
				`a`.*,
				`b`.`username` as `care_by_name`,
				`b`.`is_deleted` as `care_is_deleted`				
			FROM `crm_contacts` as `a`
				LEFT JOIN `users` as `b`
					ON `a`.`care_by_id`=`b`.`ID`
			WHERE `a`.`customer_id`='$post_id'");
		
		$contracts = $this->Model->fetchAll("SELECT 
				`a`.*,
				`c`.`title` as `parent_title`,
				`d`.`symbol` as `currency_symbol`	
			FROM `crm_contracts` as `a` 
				LEFT JOIN `crm_contract_types` as `c`
					ON `a`.`parent_id`=`c`.`ID`
				LEFT JOIN `currencies` as `d`	
					ON `a`.`currency_id`=`d`.`ID`
			WHERE `a`.`customer_id`='$post_id'");

		$trans = $this->Model->fetchAll("SELECT 
				`a`.*,
				`b`.`title` as `parent_title`,
				`c`.`username` as `created_by_name`,
				`c`.`is_deleted` as `created_is_deleted`,				
				`d`.`username` as `updated_by_name`,
				`d`.`is_deleted` as `updated_is_deleted`				
			FROM `crm_transactions` as `a` 
				LEFT JOIN `crm_transaction_types` as `b`
					ON `a`.`parent_id`=`b`.`ID`
				LEFT JOIN `users` as `c`
					ON `a`.`created_by_id`=`c`.`ID`
				LEFT JOIN `users` as `d`
					ON `a`.`updated_by_id`=`d`.`ID`					
			WHERE `a`.`customer_id`='$post_id' ORDER BY `date` DESC");

		
		$care = $this->Model->fetchRow("SELECT `username`,`ID`,`is_deleted` 
			FROM `users` WHERE `ID`='{$post['care_by_id']}'");
		
		if( $care ){
			$this->set(array(
				care => $care
			));
		}
				
	
		$this->set(array(
            post 		=>	$post,
			files 		=> $files,			
			contacts 	=> $contacts,			
			contracts   => $contracts,
			trans		=> $trans	
        ));
	}	

	public function deleteAction(){
		$ids = get('ID',array(),true);
		$user_id = get_user_id();
		
		foreach( $ids as $post_id ){
			$post = $this->Model->fetchRow("SELECT * FROM `crm_customers`
				WHERE `ID`='$post_id'");

			if( !$post  ){
				continue;
			}
			
			if( $post['created_by_id'] != $user_id AND !$this->hasRole('crm_admin') ){
				continue;
			}
			
			$this->Model->delete('crm_customers'," `ID`='$post_id' ");
			$this->Model->delete('crm_transactions'," `customer_id`='$post_id' ");
			$files = $this->Model->fetchAll("SELECT * FROM `crm_files` 
				WHERE `post_id`='$post_id' AND `mode`='customer'");
			$this->Model->delete('crm_files',"`post_id`='$post_id' AND `mode`='customer'");
					
			foreach( $files as $file ){
				@unlink("files/crm/{$file['name']}");
			}						
		}
		
		return $this->setJSON(array(
			redirect => '#'.get('url','Crm/Customer')
		));
	}
	
	public function fileAction(){
		$file_id = get('ID',0);
		$user_id = get_user_id();
		$file = $this
			->Model->fetchRow("SELECT * FROM `crm_files` as `a` WHERE `a`.`ID`='$file_id' AND `mode`='customer' LIMIT 1");

		if(!$file){
			return $this->setContent(error('File đã bị xóa hoặc không tồn tại'));
		}
		$post  = $this->Model
			->fetchRow("SELECT * FROM `crm_customers` WHERE `ID`='{$file['post_id']}' AND `is_draft`='no'");
			
		if(!$post){
			return $this->setContent(error('File đã bị xóa hoặc không tồn tại'));
		}		
		
		if( $post['created_by_id'] != $user_id AND !$this->hasRole('crm_admin') AND $post['care_by_id'] != $user_id){
			return $this->setContent(error('Bạn không có quyền xem file này'));			
		}				
		
		$file_path = "files/crm/{$file['name']}";
		
		if( !file_exists( $file_path ) ){
			return $this->setError('404');
		}	
		if( get('type') == 'icon' ){	
			$img_types = array('image/gif','image/png','image/x-png','image/jpeg','image/x-jpeg','image/jpg');
			if( in_array($file['type'],$img_types) ){
				loadClass('PHPThumb');
				$thumb = PhpThumbFactory::create($file_path);
				$thumb->resize(60, 60);
				$thumb->show();
			}else{
				@readfile("files/static/icon.gif");	
			}		
		}else if( get('type') == 'view' ){	
			$img_types = array('image/gif','image/png','image/x-png','image/jpeg','image/x-jpeg','image/jpg');
			if( in_array($file['type'],$img_types) ){
				loadClass('PHPThumb');
				$thumb = PhpThumbFactory::create($file_path);
				$thumb->show();
			}else{
				@readfile("files/static/icon.gif");	
			}		
		}else{
			@header("Content-Disposition: attachment; filename={$file['name']}");
			@readfile($file_path);
		}
	}	

	public function searchAction(){
		if( !isPost() ){
			$this->getOptions();
		}else{
			$vars = $this->indexAction();
			if( count($vars) == 0 ){
				return $this->setJSON(array(
					alert	=> 'Chưa có thông số tìm kiếm'
				));
			}else{
				return $this->setJSON(array(
					redirect	=> '#Crm/Customer?'.to_query_configs($vars,false)
				));
			}
		}
	}
	
	public function exportAction(){
		$this->removeLayout();
		$this->setView('_FormExport');
		$fields = array(
			ID				=> array('ID',true),
			code			=> array('Mã KH',true),
			name			=> array('Tên',true),
			trade_title		=> array('Ngành nghề'),		
			country_title	=> array('Quốc gia'),
			state_title		=> array('Tỉnh thành'),
			district_title	=> array('Quận huyện'),
			address			=> array('Trụ sở'),
			phone			=> array('Điện thoại',true),
			fax				=> array('Fax',true),
			email			=> array('Email',true),
			website			=> array('Website'),
			birthday		=> array('Sinh nhật'),
			desc			=> array('Mô tả'),
			care_by_name	=> array('Người chăm sóc',true),
			parent_title	=> array('Nhóm khách hàng',true)
		);
			
		if( !isPost() ){	
			$this->set(array(
				fields	=> $fields
			));		
		
		}else{
			$ids = get('ID',array(),true);
		
			if( count($ids) == 0){
				die("<script>
					window.parent.$.Alert('Chưa có bản ghi nào được chọn');
				</script>");
			}			

			$ids = join(",",$ids);	
			
			$posts = $this->Model->fetchAll("
				SELECT `a`.*,
					`b`.`username` as `care_by_name`,
					`d`.`title` as `trade_title`, 
					`e`.`title` as `parent_title`, 
					`f`.`title` as `country_title`, 
					`g`.`title` as `state_title`, 
					`h`.`title` as `district_title`,
					IF(IFNULL(`a`.`birthday`,'0000-00-00')<>'0000-00-00',DATE_FORMAT(`a`.`birthday`,'%d/%m/%Y'),'') as `birthday`	
				FROM `crm_customers` as `a`
					LEFT JOIN `users` as `b` 
						ON `b`.`ID`=`a`.`care_by_id`
					LEFT JOIN `crm_trade_types` as `d` 
						ON `d`.`ID`=`a`.`trade_id`
					LEFT JOIN `crm_customer_types` as `e` 
						ON `e`.`ID`=`a`.`parent_id`
					LEFT JOIN `locations` as `f` 
						ON `f`.`ID`=`a`.`country_id` AND `f`.`type`='1'
					LEFT JOIN `locations` as `g` 
						ON `g`.`ID`=`a`.`state_id` AND `g`.`type`='2'
					LEFT JOIN `locations` as `h` 
						ON `h`.`ID`=`a`.`district_id` AND `h`.`type`='3'
					WHERE
						`a`.`ID` IN ($ids) AND `a`.`is_draft`='no'
			");	

			$type = get('filetype','xml');		
			loadClass('ZExport');
			
			$export = new ZExport();
			$columns = get('fields',array(),true);

			$export->set($fields, $columns, $posts, $type, get('order') );
			$export->setTitle('Danh sách công ty');
			$export->setFile('Office_Export_Crm_Customer');
			$export->export();			
		}	
	}	

	public function changecareAction(){
		$this->removeLayout();
		
		$user_id = get_user_id();	
		$this->getOptions();
		if( !$this->hasRole('crm_admin') ){
			return $this->setJSON(array(
				alert => 'Bạn không có quyền thực hiện tác vụ này'
			));
		}
		
		$cus_ids = get('ID',array(),true);
		if( count($cus_ids) == 0 ){
			return $this->setJSON(array(
				alert => 'Chưa chọn công ty'
			));			
		}
		
		$x=array();
		foreach( $cus_ids as $k ){
			$x[] = "'$k'";
		}
		
		$x = array_unique($x);
		$x = implode(',',$x);
		
		$customers = $this->Model->fetchAll("SELECT `name`,`ID` FROM `crm_customers`
			WHERE `ID` IN ($x) AND `is_draft`='no'");	
		
		$this->set(array(
			customers => $customers
		));	
		
		if(isPost()){		
			$meta = array();
			$items = get('title',array(),true);
			loadClass('ZData');
			$f = new ZData();
			$f->addFields(array(
				care_by_id	=> array(
					type			=>	'INT',	
					no_empty		=> true,
					form_label		=>	'Người dùng'
				)			
			));
			
			$data = $f->getData();
			
			if( !is_array($data)){
				return $this->setJSON(array(
					alert => $data
				));
			}		
			
			$this->Model->update('crm_customers',$data,"`ID` IN ($x)");
			
			return $this->setJSON(array(
				close	=> true,
				reload	=> true
			));
		}
	}

}
?>